package com.ks.app.admin.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ks.app.admin.hibernate.KMerBaseSet;
import com.ks.app.admin.hibernate.KsMerAuthen;
import com.ks.app.admin.hibernate.KsMerchantRegist;
import com.ks.app.admin.hibernate.KsOperaterLog;
import com.ks.app.admin.service.KsoperatelogService;
import com.ks.app.admin.service.MerchantService;
import com.ks.app.admin.util.Pager;
import com.ks.app.logger.Log4jUtil;

/***
 * 每个serviceimpl 必须实现BaseServiceImpl
 * 
 * @author Administrator
 * 
 */
@Service("merchantRgService")
public class MerServiceImpl extends BaseServiceImpl implements MerchantService {

	private String className = "MerServiceImpl.";
	@Resource
	private KsoperatelogService ksoperatelogService;
	private Logger slogger = Log4jUtil.getSQLLogger();
	private Logger blogger  = Log4jUtil.getBusinessLogger();

	public KsoperatelogService getKsoperatelogService() {
		return ksoperatelogService;
	}

	public void setKsoperatelogService(KsoperatelogService ksoperatelogService) {
		this.ksoperatelogService = ksoperatelogService;
	}

	@SuppressWarnings("unchecked")
	public List<KsMerchantRegist> GetKsMer(String mno) {
		
		String method = "getKsMer(String uiid)";
		
		blogger.info(className + method + "query the merchant by uiid--");
		String sql = "from KsMerchantRegist where ksMerNo='" + mno + "'";
		return em.createQuery(sql).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<KsMerchantRegist> GetKsMerList(String uiid, Pager pager) {
		String sql = " from KsMerchantRegist where 1=1 ";
		if (uiid != null && uiid.length() > 0) {
			sql = sql + "  and ksMerRegistId=? ";
		}
		Query query = em.createQuery(sql + " order by ksMerRegistId desc ");
		if (uiid != null && uiid.length() > 0) {
			long mid = Long.parseLong(uiid);
			query.setParameter(1, mid);
		}
		query = em.createQuery(sql);
		// 在设置query的分页属性之前先设置paper的总记录数
		pager.setTotoalCnt(query.getResultList().size());
		query.setMaxResults(pager.getCntOfPage());
		query.setFirstResult((pager.getCurPage() - 1) * pager.getCntOfPage());
		List<KsMerchantRegist> list = query.getResultList();
		System.out.println("默认查询信息的个数：" + list.size());
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<KsMerchantRegist> GetKsMer(String username, String pwd) {

		String method = "GetKsMer(String username, String pwd)";

		String sql = "";
		if (pwd != null && !"".equals(pwd)) {
			blogger.info("根据用户名密码查询");
			sql = "from KsMerchantRegist  where merName='" + username
					+ "' and ksPwd='" + pwd + "'";
			//sql = "select * from K_MERCHANTREGIST where mername='"+username+"' and kspwd='"+pwd +"'";
		} else {
			sql = "from KsMerchantRegist k where k.merName='" + username + "'";
		}
		slogger.info(className + method +"show the sql!!!!---------"+sql);
		List<KsMerchantRegist> list = em.createQuery(sql).getResultList();
		blogger.info(className + method + "get the length of list = " + list.size());
		return list;

	}

	public List<KsMerchantRegist> GetKsMerByMerGroup(String Group) {
		return null;
	}

	// 查询商家信息------------
	@SuppressWarnings("unchecked")
	public List<KsMerchantRegist> GetKsMerByMername(String mername, Pager pager) {
		String sql = "";
		Query query = null;
		if (!"".equals(mername) && mername != null) {
			// 按商家名字查询
			sql = "from KsMerchantRegist k where k.ksFullName='" + mername
					+ "'";
			query = em.createQuery(sql);
			pager.setTotoalCnt(query.getResultList().size());
			query.setMaxResults(pager.getCntOfPage());
			query.setFirstResult((pager.getCurPage() - 1)
					* pager.getCntOfPage());
			return query.getResultList();
		}
		return null;
	}

	// 按条件查询
	@SuppressWarnings("unchecked")
	public List<KsMerchantRegist> getKsMerByTerm(String uiid, String optionId,
			Pager pager) {
		System.out.println("query by optionId!-----------------");
		String sql = "";
		Query query = null;
		if (optionId.equals("1")) {
			sql = "from KsMerchantRegist k order by k.ksCreateTime desc ";
		} else if (optionId.equals("2")) {
			sql = "from KsMerchantRegist k order by ksMerArea desc,ksCreateTime desc";
		} else if (optionId.equals("3")) {
			sql = "from KsMerIntegrity k order by k.merSalerroom desc";
		} else if (optionId.equals("4")) {
			sql = "from KsMerchantRegist k order by k.KsMerCredit desc";
		}
		query = em.createQuery(sql);
		pager.setTotoalCnt(query.getResultList().size());
		query.setMaxResults(pager.getCntOfPage());
		query.setFirstResult((pager.getCurPage() - 1) * pager.getCntOfPage());
		return query.getResultList();
	}

	// ---------------

	public long addKsMer(KsMerchantRegist KsMer, String mername) {
		try {
			// 添加日志
			String uno = KsMer.getKsMerNo();
			String uname = KsMer.getMerName();
			// long uid=KsUser.getKsUserId();
			String gp = KsMer.getKsMerGroup();
			String cname = KsMer.getKsFullName();
			em.persist(KsMer);
			long res = KsMer.getKsMerRegistId();
			KsOperaterLog ksOperateLog = new KsOperaterLog();
			ksOperateLog.setContent(mername + "添加了用户:[id=" + res + "];[编号="
					+ uno + "]；[商家全称=" + cname + "];[登录名称=" + uname + "]；用户组="
					+ gp);
			ksOperateLog.setLogtime(new Date());
			ksOperateLog.setOperater(mername);
			ksoperatelogService.save(ksOperateLog);
			return res;
		} catch (Exception ee) {
			ee.printStackTrace();
			return 0;
		}
	}

	public int deleteKsMer(KsMerchantRegist ksmer, String mername) {
		em.remove(em.merge(ksmer));
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<KsMerchantRegist> getKsMer() {
		String sql = "from KsMerchantRegist";
		return em.createQuery(sql).getResultList();
	}

	public String login(String ksmer) {
		return null;
	}

	public int saveKsMer(KsMerchantRegist ksmer) {
		return 0;
	}

	public long updateKsMer(KsMerchantRegist KsMer, String mername) {
		
		String method = "updateKsMer(KsMerchantRegist KsMer, String mername)";
		
		long res = -1;
		try {
			String uno = KsMer.getKsMerNo();
			String uname = KsMer.getMerName();
			long uid = KsMer.getKsMerRegistId();
			String gp = KsMer.getKsMerGroup();
			String fname = KsMer.getKsFullName();
			System.out.println(KsMer.getKsMerRegistId()+"00000000"+KsMer.getMerRoom());
			em.merge(KsMer);
			blogger.info(className + method + "merge KsMer");
			res = 1;

			KsOperaterLog ksOperateLog = new KsOperaterLog();

			ksOperateLog.setContent(mername + "修改了用户:[id=" + uid + "];[编号="
					+ uno + "]；[中文名称=" + fname + "];[登录名称=" + uname + "]；用户组="
					+ gp);
			ksOperateLog.setLogtime(new Date());
			ksOperateLog.setOperater(mername);
			ksoperatelogService.save(ksOperateLog);
		} catch (Exception e) {

		}

		return res;
	}

	@Override
	public List<KsMerAuthen> GetAuthenList() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<KsMerAuthen> GetAuthenByMername(String mername, Pager pager) {
		String sql = "";
		Query query = null;
		if (!"".equals(mername) && mername != null) {
			// 按商家名字查询
			sql = "from KsMerAuthen";
			query = em.createQuery(sql);
			pager.setTotoalCnt(query.getResultList().size());
			query.setMaxResults(pager.getCntOfPage());
			query.setFirstResult((pager.getCurPage() - 1)
					* pager.getCntOfPage());
			return query.getResultList();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<KsMerAuthen> GetAuthen() {
		String sql = "from KsMerAuthen";
		return em.createQuery(sql).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<KsMerAuthen> GetAuthenList(String uiid, Pager pager) {
		String sql = " from KsMerAuthen where 1=1 ";
		if (uiid != null && uiid.length() > 0) {
			sql = sql + "  and ksMerRegistId=? ";
		}
		Query query = em.createQuery(sql + " order by ksMerRegistId desc ");
		if (uiid != null && uiid.length() > 0) {
			long mid = Long.parseLong(uiid);
			query.setParameter(1, mid);
		}
		query = em.createQuery(sql);
		// 在设置query的分页属性之前先设置paper的总记录数
		pager.setTotoalCnt(query.getResultList().size());
		query.setMaxResults(pager.getCntOfPage());
		query.setFirstResult((pager.getCurPage() - 1) * pager.getCntOfPage());
		List<KsMerAuthen> list = query.getResultList();
		System.out.println("默认查询信息的个数：" + list.size());
		return list;
	}

	@Override
	public long updateSet(KMerBaseSet baseSet, String mername) {
		
		String method = "updateSet()";
		blogger.info(className + method + "come in update the baseSet");
		
		KMerBaseSet set = em.merge(baseSet);
		if(set != null ){
			em.merge(set);
			blogger.info(mername + "operate " +className + method + "update the setting successed");
			return 1;
		}
		return 0;
	}
}
